<?
require_once('./lib/authentication.lib.php');
require_once('./lib/sort.lib.php');
require_once('./lib/game.lib.php');

if (!$_SESSION["user"]) die("You have to be logged in");

$max_squad = 23;
?>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
	        "http://www.w3.org/TR/REC-html40/loose.dtd">
	<HTML>
	<HEAD>
	<SCRIPT TYPE="text/javascript" SRC="js/game.js"></SCRIPT>
	<SCRIPT TYPE="text/javascript" SRC="js/game_squad.js"></SCRIPT>
	<SCRIPT TYPE="text/javascript" SRC="js/edit_squad.js"></SCRIPT>
<?

if (isset($_GET["team"]) and isset($_GET["game"]))
{
	$team_id = mysql_escape_string($_GET["team"]);
	$game_id = mysql_escape_string($_GET["game"]);

	$squads = array();

	$query = "SELECT Championship.id, Championship.name, Championship.begin, Championship.end FROM Championship, Phase, Game WHERE Championship.id = Phase.championship AND Phase.id = Game.phase AND Game.id = '$game_id';";
	$result_champ = mysql_query($query);
	if (mysql_numrows($result_champ) != 1)
	{
		die("Invalid champ");
	}

	$champ_id = mysql_result($result_champ, 0, "id");
	$champ_name = mysql_result($result_champ, 0, "name");
	$champ_begin = mysql_result($result_champ, 0, "begin");
	$champ_end = mysql_result($result_champ, 0, "end");

	$champ_begin = substr($champ_begin, 0, 4);
	$champ_end = substr($champ_end, 0, 4);
	?>
	<?
	echo "<TITLE>$champ_name $champ_begin" . (($champ_begin !== $champ_end) ? "/$champ_end" : "") . "</TITLE>\n";
	?>
	<link rel="stylesheet" href="css/style.css" type="text/css">
	</HEAD>
	<BODY>

	<FORM ACTION="edit_squad.php" METHOD="POST" NAME="squad">
	<INPUT TYPE="hidden" NAME="team" VALUE="<?echo $team_id?>">
	<INPUT TYPE="hidden" NAME="game" VALUE="<?echo $game_id?>">
	<TABLE>
	<TR><TD>
	<SELECT MULTIPLE SIZE='20' NAME='team_players' onDblClick='edit_player(this.options[this.options.selectedIndex].value);'>
	<?
	$query = "SELECT Player.name, Player.id, Player.position, full_name, `on`, off, yellow, red FROM Player, player_game WHERE player_game.player = Player.id AND player_game.game = '$game_id' AND player_game.team = '$team_id';";
	$result_squad = mysql_query($query);
	$num_squad = mysql_numrows($result_squad);
	for ($i = 0; $i < $num_squad; $i++)
	{
		$player_id = mysql_result($result_squad, $i, "id");
		$player_name = mysql_result($result_squad, $i, "name");
		$player_fullname = mysql_result($result_squad, $i, "full_name");
		$player_position = mysql_result($result_squad, $i, "position");
		$on = mysql_result($result_squad, $i, "on");
		$off = mysql_result($result_squad, $i, "off");
		$yellow = mysql_result($result_squad, $i, "yellow");
		$red = mysql_result($result_squad, $i, "red");

		$squads[$player_id] = array("name" => $player_name, "position" => $player_position, "on" => $on, "off" => $off, "yellow" => $yellow, "red" => $red, "full_name" => $player_fullname);
	}

	$squads = multi_sort($squads, array(array("on", true), array("position", true, "position_sort"), array("name", true)), true);
	
	$query = "SELECT Player.name, Player.id, Player.position, Player.full_name FROM Player, team_player WHERE team_player.player = Player.id AND team_player.team = '$team_id' AND team_player.champ = '$champ_id' ORDER BY Player.full_name, Player.name;";
	$result_player = mysql_query($query);
	$num_player = mysql_numrows($result_player);
	$players = array();
	for ($i = 0; $i < $num_player; $i++)
	{
		$player_id = mysql_result($result_player, $i, "Player.id");
		$player_name = mysql_result($result_player, $i, "Player.name");
		$player_fullname = mysql_result($result_player, $i, "Player.full_name");
		$player_position = mysql_result($result_player, $i, "Player.position");

		if (!isset($squads[$player_id]))
			echo "<OPTION VALUE='$player_id'>$player_fullname ($player_name) - $player_position</OPTION>\n";
	}
	?>
	</SELECT>
	<BR>
	<INPUT TYPE="button" VALUE="New player" onClick="new_player_squad(<?echo $champ_id?>, <?echo $team_id?>, '');"><BR>
	<INPUT TYPE="button" VALUE="Add all" onClick="add_all_squad();">
	</TD>

	<TD>
	<INPUT TYPE="button" VALUE=">>" onClick="add_squad_player();"><BR>
	<INPUT TYPE="button" VALUE="<<" onClick="rem_squad_player();">
	</TD>

	<TD>
	<TABLE ID="squad_players">
	<TR><TH>Player</TH><TH>On</TH><TH>Off</TH><TH>Y</TH><TH>R</TH></TR>
	<?
	for ($i = 0; $i < $max_squad; $i++)
	{
		echo "<TR>\n";
		if (list($key, $val) = each($squads))
		{
			echo "<TD><SELECT MULTIPLE SIZE='1' NAME='player_$i' STYLE='width: 200px' class='squad_player' onClick='select_squad_player(this);' onDblClick='rem_squad_player();'>\n";
			echo "<OPTION VALUE='$key'>";
			echo $val["name"] . " - " . $val["position"];
			echo "</OPTION>\n";
			echo "</SELECT></TD>\n";
			echo "<TD><INPUT TYPE='text' NAME='on_$i' SIZE='1' value='";
			echo $val["on"];
			echo "' onChange='document.squad.off_$i.value = 90;'></TD>\n";
			echo "<TD><INPUT TYPE='text' NAME='off_$i' SIZE='1' value='";
			echo $val["off"];
			echo "'></TD>\n";

			echo "<TD><INPUT TYPE='checkbox' NAME='yellow_$i'";
			if ($val["yellow"]) echo " checked";
			echo "></TD>\n"; 
			echo "<TD><INPUT TYPE='checkbox' NAME='red_$i'";
			if ($val["red"]) echo " checked";
			echo "></TD>\n"; 
		}
		else
		{
			echo "<TD><SELECT MULTIPLE SIZE='1' NAME='player_$i' STYLE='width: 200px' class='squad_player' onClick='select_squad_player(this);' onDblClick='rem_squad_player();'>\n";
			echo "</SELECT></TD>\n";
			if ($i < 11)
			{
				echo "<TD style='font-size: 75%'>0<INPUT TYPE='hidden' NAME='on_$i' VALUE='0'></TD>\n";
				echo "<TD><INPUT TYPE='text' NAME='off_$i' SIZE='1' value='90'></TD>\n";
			}
			else
			{
				echo "<TD><INPUT TYPE='text' NAME='on_$i' SIZE='1' value='0' onChange='document.squad.off_$i.value = 90;'></TD>\n";
				echo "<TD><INPUT TYPE='text' NAME='off_$i' SIZE='1' value='0'></TD>\n";
			}

			echo "<TD><INPUT TYPE='checkbox' NAME='yellow_$i'></TD>\n"; 
			echo "<TD><INPUT TYPE='checkbox' NAME='red_$i'></TD>\n"; 
		}
		echo "</TR>\n";
	}
	?>
	</TABLE>
	</TD>
	</TR></TABLE>
	<INPUT TYPE="button" VALUE="Send" onClick="squad_send()">
	</FORM>
	</BODY>
	</HTML>
	<?
}
else if (isset($_POST["game"]) and isset($_POST["team"]))
{
	$game_id = mysql_escape_string($_POST["game"]);
	$team_id = mysql_escape_string($_POST["team"]);

	$query = "DELETE FROM player_game WHERE game = '$game_id' and team = '$team_id'";
	mysql_query($query) or die("Can't insert squad");

	for ($i = 0; $i < $max_squad; $i++)
	{
		if (isset($_POST["player_$i"]))
		{
			$player_id = mysql_escape_string($_POST["player_$i"]);
			$on = mysql_escape_string($_POST["on_$i"]);
			$off = mysql_escape_string($_POST["off_$i"]);
			$yellow = isset($_POST["yellow_$i"]) ? 1 : 0;
			$red = isset($_POST["red_$i"]) ? 1 : 0;

			$query = "INSERT INTO player_game VALUES ('$player_id', '$game_id', '$team_id', '$on', '$off', '$yellow', '$red');";
			mysql_query($query) or die("Can't insert squad");
		}
	}

	?>
<SCRIPT>
<!--
window.opener.write_squad(window.opener.document.getElementById('game_squad_' + <?echo $game_id?>), <?echo $game_id?>, window);
-->
</SCRIPT>
</HEAD>
<BODY></BODY>
</HTML>
	<?
}
